<?php

namespace app\admin\controller;

use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\response\Json;
/**
 * 机器人事件
 *
 * admin-RobotEvent
 *
 * @Apidoc\Title("机器人事件")
 * @Apidoc\Group("api")
 */
class RobotEvent extends BaseController
{
    /**
     * 新增
     *
     * admin-RobotEvent-add
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\RobotEvent::create($data['robot_event']);
            return success();
        } else {
            $options = [];
            $robot_event__robot_id_option = \app\common\model\Robot::column('name as name, id as id');
            $options['robot_event__robot_id_options'] = $robot_event__robot_id_option;
            $robot_event__is_train_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['robot_event__is_train_options'] = $robot_event__is_train_option;
            $robot_event__is_merge_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['robot_event__is_merge_options'] = $robot_event__is_merge_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 修改
     *
     * admin-RobotEvent-update
     */
    public function update()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\RobotEvent::update($data['robot_event']);
            return success();
        } else {
            $data = \app\common\model\RobotEvent::find(input('robot_event__id'));
            $r = [];
            foreach ($data->getData() as $key => $val) {
                $r['robot_event__' . $key] = $val;
            }
            View::assign('r', $r);
            $options = [];
            $robot_event__robot_id_option = \app\common\model\Robot::column('name as name, id as id');
            $options['robot_event__robot_id_options'] = $robot_event__robot_id_option;
            $robot_event__is_train_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['robot_event__is_train_options'] = $robot_event__is_train_option;
            $robot_event__is_merge_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['robot_event__is_merge_options'] = $robot_event__is_merge_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 删除
     *
     * admin-RobotEvent-delete
     */
    public function delete()
    {
        \app\common\model\RobotEvent::destroy(input('robot_event__id'));
        return success();
    }
    /**
     * 列表
     *
     * admin-RobotEvent-list
     */
    public function list()
    {
        if (request()->isPost()) {
            return error('请求方式错误');
        }
        $query = Db::table('db_robot_event');
        $query->join('db_robot', 'db_robot.id = db_robot_event.robot_id');
        if (input('robot_event__name', '') !== '') {
            $query->where('db_robot_event.name', 'like', '%' . input('robot_event__name', '', 'search_in') . '%');
        }
        if (input('robot__id', '') !== '') {
            $query->where('db_robot.id', '=', input('robot__id', '', 'search_in'));
        }
        $query->where('db_robot_event.delete_time', '=', null);
        $query->where('db_robot.delete_time', '=', null);
        $query->field(['db_robot_event.id' => 'robot_event__id']);
        $query->field(['db_robot_event.name' => 'robot_event__name']);
        $query->field(['db_robot_event.trigger_text' => 'robot_event__trigger_text']);
        $query->field(['db_robot_event.type' => 'robot_event__type']);
        $query->field(['db_robot_event.form_field' => 'robot_event__form_field']);
        $query->field(['db_robot.name' => 'robot__name']);
        $query->field(['db_robot_event.is_train' => 'robot_event__is_train']);
        $query->field(['db_robot_event.is_merge' => 'robot_event__is_merge']);
        $query->field(['db_robot_event.form_name' => 'robot_event__form_name']);
        $query->order('db_robot_event.id', 'desc');
        $data = $query->paginate(input('listRow') ?: 15);
        $data = ['data' => $data->items(), 'paginate' => $data->render()];
        View::assign($data);
        $options = [];
        $robot__id_option = \app\common\model\Robot::column('name as name, id as id');
        $options['robot__id_options'] = $robot__id_option;
        $robot_event__is_train_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
        $options['robot_event__is_train_options'] = $robot_event__is_train_option;
        $robot_event__is_merge_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
        $options['robot_event__is_merge_options'] = $robot_event__is_merge_option;
        View::assign($options);
        return View::fetch();
    }
}